//
//  LYAreaController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/13.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYAreaController.h"
#import "LYAreaLeftCell.h"
#import "LYAreaRightCell.h"

@interface LYAreaController ()<UITableViewDataSource,UITableViewDelegate>

//列表
@property (nonatomic, strong) UITableView *leftTable;
@property (nonatomic, strong) UITableView *rightTable;

//定位
@property (nonatomic, assign) NSInteger leftIndex;
@property (nonatomic, assign) NSInteger rightIndex;

@end

@implementation LYAreaController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self createSubview];
}

- (void)createSubview{
    self.leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/3, 300) style:UITableViewStyleGrouped];
    self.leftTable.delegate = self;
    self.leftTable.dataSource = self;
    self.leftTable.showsVerticalScrollIndicator = NO;
    self.leftTable.estimatedRowHeight = 0;
    self.leftTable.estimatedSectionHeaderHeight = 0;
    self.leftTable.estimatedSectionFooterHeight = 0;
    self.leftTable.backgroundColor = THEMECOLOR_OF_GRAY;
    [self.view addSubview:self.leftTable];
    self.leftIndex = 0;
    [self.leftTable registerClass:[LYAreaLeftCell class] forCellReuseIdentifier:kCellIdentifier_LYAreaLeftCell];
    
    self.rightTable = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH/3, 0, WIDTH*2/3, 300) style:UITableViewStyleGrouped];
    self.rightTable.delegate = self;
    self.rightTable.dataSource = self;
    self.rightTable.showsVerticalScrollIndicator = NO;
    self.rightTable.estimatedRowHeight = 0;
    self.rightTable.estimatedSectionHeaderHeight = 0;
    self.rightTable.estimatedSectionFooterHeight = 0;
    self.rightTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.rightTable];
    
    [self.rightTable registerClass:[LYAreaRightCell class] forCellReuseIdentifier:kCellIdentifier_LYAreaRightCell];
    self.rightIndex = 0;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.leftTable) {
        if (self.dataArray && self.dataArray.count) {
            return self.dataArray.count;
        }
        return self.itemDataArray.count;
    } else {
        if (self.dataArray && self.dataArray.count) {
            NSDictionary *secDic = self.dataArray[self.leftIndex];
            NSArray *secArray = secDic[@"child"];
            return secArray.count;
        }
        PublicCategoryElementModel *secModel = self.itemDataArray[self.leftIndex];
        NSArray *secArray = secModel.childArray;
        return secArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTable) {
        LYAreaLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYAreaLeftCell forIndexPath:indexPath];
        if (self.dataArray && self.dataArray.count) {
            [cell setDataDic:self.dataArray[indexPath.row] isSelect:((self.leftIndex == indexPath.row)?YES:NO)];
        } else {
            [cell reloadData:self.itemDataArray[indexPath.row] isSelect:((self.leftIndex == indexPath.row)?YES:NO)];
        }
        return cell;
    } else {
        LYAreaRightCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYAreaRightCell forIndexPath:indexPath];
        if (self.dataArray && self.dataArray.count) {
            NSDictionary *secDic = self.dataArray[self.leftIndex];
            NSArray *secArray = secDic[@"child"];
            [cell setDataDic:secArray[indexPath.row] isSelect:((self.rightIndex == indexPath.row)?YES:NO)];
        } else {
            PublicCategoryElementModel *secModel = self.itemDataArray[self.leftIndex];
            NSArray *secArray = secModel.childArray;
            [cell reloadData:secArray[indexPath.row] isSelect:((self.rightIndex == indexPath.row)?YES:NO)];
        }
        return cell;
    }
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTable) {
        if (indexPath.row != self.leftIndex) {
            self.leftIndex = indexPath.row;
            [self.leftTable reloadData];
            self.rightIndex = 0;
            [self.rightTable reloadData];
        }
    } else {
        self.rightIndex = indexPath.row;
        [self.rightTable reloadData];
        
        if (self.dataArray && self.dataArray.count) {
            NSDictionary *dic1 = self.dataArray[self.leftIndex];
            NSArray *secArray = dic1[@"child"];
            NSDictionary *dic2 = secArray[indexPath.row];
            _blcok(dic1, dic2);
        } else {
            PublicCategoryElementModel *leftModel = self.itemDataArray[self.leftIndex];
            NSArray *secArray = leftModel.childArray;
            PublicCategoryElementModel *rightModel = secArray[indexPath.row];
            self.backBlcok(leftModel, rightModel);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

@end
